package com.wcs.shortlink.admin.util;

import java.util.Random;

/**
 * 随机生成分组id工具类
 */
public final class RandomCodeUtil {
    // 定义所有可能的字符（去掉容易混淆的字符如0/O,1/I等）
    private static final String ALL_CHAR = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
    private static final Random RANDOM = new Random();

    /**
     * 生成6位随机字母数字混合字符串
     * @return 6位随机字符串
     */
    public static String generate6DigitCode() {
        StringBuilder sb = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            int index = RANDOM.nextInt(ALL_CHAR.length());
            sb.append(ALL_CHAR.charAt(index));
        }
        return sb.toString();
    }

    /**
     * 生成指定位数的随机字母数字混合字符串
     * @param length 要生成的字符串长度
     * @return 随机字符串
     */
    public static String generateRandomCode(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be positive");
        }

        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(ALL_CHAR.length());
            sb.append(ALL_CHAR.charAt(index));
        }
        return sb.toString();
    }

}
